package com.example.makerspace.manager;

import com.example.makerspace.dao.DemandDAO;
import com.example.makerspace.domain.DemandDO;
import com.example.makerspace.dto.*;
import com.example.makerspace.enums.ResultCodeEnum;
import com.example.makerspace.utils.FileUtils;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.example.makerspace.service.impl.AdminServiceImpl.managerMap;

@Component
public class DemandManager extends BaseManager<DemandDO, DemandDTO>{
    private final static String DEMAND_PICTURE_PATH_PREFIX = "demand/";
    private final static List<Map<String, String>> FILTER_PARAMS = Lists.newArrayList(
            ImmutableMap.<String, String>builder().put(QUERY_FIELD, "category").put(QUERY_TYPE, QUERY_TYPE_MATCH_PHRASE).build()
    );
    private final static List<Map<String, String>> FUZZY_QUERY_PARAMS = Lists.newArrayList(
            ImmutableMap.<String, String>builder().put(QUERY_FIELD, "demand_name").put(QUERY_TYPE, QUERY_TYPE_MATCH_PHRASE).build(),
            ImmutableMap.<String, String>builder().put(QUERY_FIELD, "keyword").put(QUERY_TYPE, QUERY_TYPE_MATCH_PHRASE).build(),
            ImmutableMap.<String, String>builder().put(QUERY_FIELD, "demand_desc").put(QUERY_TYPE, QUERY_TYPE_MATCH_PHRASE).build()
    );
  private  final  static  String   testHttpPrefix="https://119.45.199.13:3035/static/";
   private  final  static  String  devHttpPrefix="https://nism.ytcata.com:443/static/";
    @Resource
    private DemandDAO demandDAO;

    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @PostConstruct
    public void init(){
        this.baseDAO = demandDAO;
        managerMap.put("demand", this);
    }

    public Result addDemand(DemandDTO demandDTO){
        ArrayList<String> imagePaths = new ArrayList<>();
        DemandDO oldDemand = demandDAO.selectByDemandName(demandDTO.getDemandName());
        if(oldDemand != null){
            return Result.error(ResultCodeEnum.DEMAND_EXIST);
        }
        if(demandDTO.getPicture().size()!=0){
            for(int i=0;i<demandDTO.getPicture().size();i++){
                String imagePath = FileUtils.saveMultipartFile2(demandDTO.getPicture().get(i), devHttpPrefix + DEMAND_PICTURE_PATH_PREFIX + demandDTO.getDemandName(),
                        devHttpPrefix+DEMAND_PICTURE_PATH_PREFIX+demandDTO.getDemandName());
                imagePaths.add(imagePath);
            }
        }
        DemandDO demandDO = new DemandDO();
        BeanUtils.copyProperties(demandDTO, demandDO);
        demandDO.setPicture(imagePaths);
        demandDO.setIsShow(demandDTO.getIsShow());
        demandDAO.insert(demandDO);
        return Result.success();
    }

    public Result updateDemand(DemandDTO demandDTO){
        ArrayList<String> imagePaths = new ArrayList<>();
        DemandDO demandDO = demandDAO.selectById(demandDTO.getId());
        if(demandDO == null){
            return Result.error(ResultCodeEnum.DEMAND_NOT_EXIST);
        }
        Optional.ofNullable(demandDTO.getDemandName()).ifPresent(demandDO::setDemandName);
        Optional.ofNullable(demandDTO.getIsShow()).ifPresent(demandDO::setIsShow);
        Optional.ofNullable(demandDTO.getCategory()).ifPresent(demandDO::setCategory);
        Optional.ofNullable(demandDTO.getKeyword()).ifPresent(demandDO::setKeyword);
        Optional.ofNullable(demandDTO.getPicture()).ifPresent(picture ->{
            FileUtils.deleteFile2(demandDO.getPicture());
            for(int i=0;i<demandDTO.getPicture().size();i++){
                String imagePath = FileUtils.saveMultipartFile2(demandDTO.getPicture().get(i), devHttpPrefix + DEMAND_PICTURE_PATH_PREFIX + demandDTO.getDemandName(),
                        FileUtils.staticPath+DEMAND_PICTURE_PATH_PREFIX+demandDTO.getDemandName());
                imagePaths.add(imagePath);
            }
        });
          demandDO.setPicture(imagePaths);
        Optional.ofNullable(demandDTO.getDemandDesc()).ifPresent(demandDO::setDemandDesc);
        return updateDO(demandDO);
    }

    @Override
    public PageResult list(PageRequest pageRequest){
        Map<String, Object> params = pageRequest.getParams();
        Pagination pagination = pageRequest.getPagination();
        String category = MapUtils.getString(params, "category");
        String fuzzy = MapUtils.getString(params, "fuzzy");
        return search(FILTER_PARAMS, category, FUZZY_QUERY_PARAMS, fuzzy, pagination, DemandDO.class);
    }

    public DemandDTO convert2DTO(DemandDO demandDO){
        DemandDTO demandDTO = super.convert2DTO(demandDO);
        for(int i=0;i<demandDO.getPicture().size();i++){
            String briefPath = FileUtils.getBriefPath(demandDO.getPicture().get(i));
            String path = devHttpPrefix + briefPath;
            demandDTO.setPicturePath(path);
        }
        return demandDTO;
    }

}
